﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public partial class User_Shares_AllProjectsDetails : System.Web.UI.Page
{
    //private int _TotalPages;


    //public int TotalPages
    //{
    //    get { return _TotalPages; }
    //    set
    //    {
    //        _TotalPages = value;
    //        Session[ "TotalPages" ] = value;
    //    }
    //}


    //private int _CurrentPage;


    //public int CurrentPage
    //{
    //    get { return _CurrentPage; }
    //    set
    //    {
    //        Session[ "PageIndex" ] = value;
    //        _CurrentPage = value;
    //    }
    //}


    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            var thisUser = HttpContext.Current.User;
            if ( thisUser.Identity.IsAuthenticated )
            {
                PopulateGrid( thisUser );
            }
        }
        catch
        {
        }
    }

    protected void ButtonSeeMore_Click( object sender, EventArgs e )
    {
        Session[ "ContractId" ] = ( ( Button ) sender ).CommandArgument.ToString();
        Response.Redirect( @"~\User\Shares\ProjectStatusDetails.aspx" );
    }


    public void PopulateGrid( System.Security.Principal.IPrincipal thisUser )
    {
        DropDownList downList = (LoginViewSearch.FindControl("DropDownListSort") as DropDownList);
        RadioButton radioButtonAsc = (LoginViewSearch.FindControl("RadioButtonAsc") as RadioButton);
        RadioButton radioButtonDec = (LoginViewSearch.FindControl("RadioButtonDec") as RadioButton);
        if ( thisUser.Identity.IsAuthenticated )
        {
            if ( thisUser.IsInRole( "admin" ) || thisUser.IsInRole("employer") || thisUser.IsInRole("edituser") || thisUser.IsInRole("viewuser"))
            {
                bool advisorSearch = ( LoginViewSearch.FindControl( "CheckBoxAdvisor" ) as CheckBox ).Checked;
                bool surveyorSearch = ( LoginViewSearch.FindControl( "CheckBoxSurveyor" ) as CheckBox ).Checked;
                List<Business.GridContractType> dataSource = new List< GridContractType >();

                dataSource = Business.ProjectDetails.GetProjectAll( advisorSearch , surveyorSearch );
                if ( radioButtonAsc != null )
                    if (radioButtonAsc.Checked)
                    {
                        if ( downList != null )
                            switch ( Convert.ToInt32( downList.SelectedValue ) )
                            {
                                case 0:
                                    dataSource = dataSource.OrderBy( project => project.VillageName ).ToList();
                                    break;
                                case 1:
                                    dataSource = dataSource.OrderBy(project => project.AdvisorFullName).ToList();
                                    break;
                                case 2:
                                    dataSource = dataSource.OrderBy(project => project.ContractNumber).ToList();
                                    break;
                                case 3:
                                    dataSource = dataSource.OrderBy(project => project.City).ToList();
                                    break;
                            }
                    }
                if ( radioButtonDec != null )
                    if ( radioButtonDec.Checked )
                    {
                        if ( downList != null )
                            switch (Convert.ToInt32(downList.SelectedValue))
                            {
                                case 0:
                                    dataSource = dataSource.OrderByDescending(project => project.VillageName).ToList();
                                    break;
                                case 1:
                                    dataSource = dataSource.OrderByDescending(project => project.AdvisorFullName).ToList();
                                    break;
                                case 2:
                                    dataSource = dataSource.OrderByDescending(project => project.ContractNumber).ToList();
                                    break;
                                case 3:
                                    dataSource = dataSource.OrderByDescending(project => project.City).ToList();
                                    break;
                            }
                    }
                GridViewProjects.DataSource = dataSource;}
            else if (thisUser.IsInRole("fourthfactor"))
            {
                bool advisorSearch = (LoginViewSearch.FindControl("CheckBoxAdvisor") as CheckBox).Checked;
                bool surveyorSearch = (LoginViewSearch.FindControl("CheckBoxSurveyor") as CheckBox).Checked;
                List<Business.GridContractType> dataSource = new List<GridContractType>();

                dataSource = Business.ProjectDetails.GetProjectByFourthFactorUserIdandrole(Membership.GetUser().ProviderUserKey, advisorSearch, surveyorSearch);
                if (radioButtonAsc != null)
                    if (radioButtonAsc.Checked)
                    {
                        if (downList != null)
                            switch (Convert.ToInt32(downList.SelectedValue))
                            {
                                case 0:
                                    dataSource = dataSource.OrderBy(project => project.VillageName).ToList();
                                    break;
                                case 1:
                                    dataSource = dataSource.OrderBy(project => project.AdvisorFullName).ToList();
                                    break;
                                case 2:
                                    dataSource = dataSource.OrderBy(project => project.ContractNumber).ToList();
                                    break;
                                case 3:
                                    dataSource = dataSource.OrderBy(project => project.City).ToList();
                                    break;
                            }
                    }
                if (radioButtonDec != null)
                    if (radioButtonDec.Checked)
                    {
                        if (downList != null)
                            switch (Convert.ToInt32(downList.SelectedValue))
                            {
                                case 0:
                                    dataSource = dataSource.OrderByDescending(project => project.VillageName).ToList();
                                    break;
                                case 1:
                                    dataSource = dataSource.OrderByDescending(project => project.AdvisorFullName).ToList();
                                    break;
                                case 2:
                                    dataSource = dataSource.OrderByDescending(project => project.ContractNumber).ToList();
                                    break;
                                case 3:
                                    dataSource = dataSource.OrderByDescending(project => project.City).ToList();
                                    break;
                            }
                    }
                GridViewProjects.DataSource = dataSource;
            }
            else if(thisUser.IsInRole( "bonyadshahrestan" ))
            {
                IDataReader dr= DataAccess.TblUserDetail.FetchByParameter(DataAccess.TblUserDetail.Columns.UserId, Membership.GetUser().ProviderUserKey);
                dr.Read();
                Guid cityid=new Guid( dr["CityId"].ToString());
                dr.Close();
                bool advisorSearch = (LoginViewSearch.FindControl("CheckBoxAdvisor") as CheckBox).Checked;
                bool surveyorSearch = (LoginViewSearch.FindControl("CheckBoxSurveyor") as CheckBox).Checked;
                List<Business.GridContractType> dataSource = new List<GridContractType>();
                Business.ProjectDetails.GetProjectByFourthFactorUserIdandrole(Membership.GetUser().ProviderUserKey, advisorSearch, surveyorSearch);
                dataSource = Business.ProjectDetails.SelectProjectsByCity(cityid);
                if (radioButtonAsc != null)
                    if (radioButtonAsc.Checked)
                    {
                        if (downList != null)
                            switch (Convert.ToInt32(downList.SelectedValue))
                            {
                                case 0:
                                    dataSource = dataSource.OrderBy(project => project.VillageName).ToList();
                                    break;
                                case 1:
                                    dataSource = dataSource.OrderBy(project => project.AdvisorFullName).ToList();
                                    break;
                                case 2:
                                    dataSource = dataSource.OrderBy(project => project.ContractNumber).ToList();
                                    break;
                                case 3:
                                    dataSource = dataSource.OrderBy(project => project.City).ToList();
                                    break;
                            }
                    }
                if (radioButtonDec != null)
                    if (radioButtonDec.Checked)
                    {
                        if (downList != null)
                            switch (Convert.ToInt32(downList.SelectedValue))
                            {
                                case 0:
                                    dataSource = dataSource.OrderByDescending(project => project.VillageName).ToList();
                                    break;
                                case 1:
                                    dataSource = dataSource.OrderByDescending(project => project.AdvisorFullName).ToList();
                                    break;
                                case 2:
                                    dataSource = dataSource.OrderByDescending(project => project.ContractNumber).ToList();
                                    break;
                                case 3:
                                    dataSource = dataSource.OrderByDescending(project => project.City).ToList();
                                    break;
                            }
                    }
                GridViewProjects.DataSource = dataSource;
            }
            else if (thisUser.IsInRole("advisor")
                 || thisUser.IsInRole("Surveyor"))
            {
                var userId = new Guid(Membership.GetUser().ProviderUserKey.ToString());
                //GridViewProjects.AllowPaging = false;
                GridViewProjects.DataSource = Business.ProjectDetails.GetProjectByUserId(userId);
            }
            Page.DataBind();
        }
    }

    protected void GridViewProjects_PreRender( object sender, EventArgs e )
    {
        if (GridViewProjects.Rows.Count > 0)
        {
            //This replaces <td> with <th> and adds the scope attribute
            GridViewProjects.UseAccessibleHeader = true;

            //This will add the <thead> and <tbody> elements
            GridViewProjects.HeaderRow.TableSection = TableRowSection.TableHeader;

            //This adds the <tfoot> element. 
            //Remove if you don't have a footer row
            GridViewProjects.FooterRow.TableSection = TableRowSection.TableFooter;
        }


    }

    protected void GridViewProjects_RowDataBound( object sender, GridViewRowEventArgs e )
    {
        if ( e.Row.RowType
             == DataControlRowType.DataRow )
        {
            if ( e.Row.RowIndex%2 == 0 )
            {
                e.Row.BackColor = System.Drawing.Color.AliceBlue;
            }
        }
        //if ( e.Row.RowType
        //     == DataControlRowType.Pager )
        //{
        //    e.Row.BackColor = System.Drawing.Color.FromArgb( 0x61, 0x84, 0xC3 );
        //    UpdatePanel updatePanel = e.Row.Cells[ 0 ].FindControl( "UpdatePanelFooter" ) as UpdatePanel;
        //    Label lbl = updatePanel.FindControl( "LabelPageNumber" ) as Label;
        //    if ( lbl != null )
        //        lbl.Text = string.Format( "{0} از {1} صفحه ", Session[ "PageIndex" ], Session[ "TotalPages" ] );
        //}
    }

    protected void CheckBox_CheckedChanged( object sender, EventArgs e )
    {
        var thisUser = HttpContext.Current.User;
        PopulateGrid( thisUser );
    }

    protected void RadioButton_CheckedChanged( object sender , EventArgs e)
    {
        var thisUser = HttpContext.Current.User;
        PopulateGrid(thisUser);
    }

    protected void DropDownChanged(object sender, EventArgs e)
    {
        var thisUser = HttpContext.Current.User;
        PopulateGrid(thisUser);
    }
     public override void VerifyRenderingInServerForm(Control control)
    {
    }
    protected void ExportToExcel_Click(object sender, EventArgs e)
    {

        try
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.Encoding.Unicode;
            Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
            //this.EnableViewState = false;
            StringWriter sr = new StringWriter();
            HtmlTextWriter hr = new HtmlTextWriter(sr);
            GridViewProjects.RenderControl(hr);
            Response.Write(sr.ToString());
            sr.Dispose();
            hr.Dispose();
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }

}